package com.huike.web.controller.contract;

import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.contract.domain.TbContract;
import com.huike.contract.domain.dto.ContactPageQueryDTO;
import com.huike.contract.domain.dto.TbContractDTO;
import com.huike.contract.domain.vo.TbContractVo;
import com.huike.contract.service.TbContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.List;

/**
 * 合同Controller
 */
@RequestMapping("/contract")
@RestController
@Slf4j
@Api("合同相关接口")
@AllArgsConstructor
public class TbContractController {

    private TbContractService tbContractService;

    /**
     * 新增合同
     * @param tbContractDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增合同")
    public AjaxResult addContract(@RequestBody TbContractDTO tbContractDTO) {
        log.info("执行新增合同的方法:{}", tbContractDTO);
        tbContractService.addContracr(tbContractDTO);
        return AjaxResult.success();
    }

    /**
     * 修改合同
     * @param tbContractDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改合同")
    public AjaxResult update(@RequestBody TbContractDTO tbContractDTO) {
        log.info("执行修改合同:");
        tbContractService.updateContract(tbContractDTO);
        return AjaxResult.success();
    }

    /**
     * 商机转合同
     * @param id
     * @param contractDTO
     * @return
     */
    @PutMapping("/changeContract/{id}")
    @ApiOperation("商机转合同")
    public AjaxResult changeToContract (@PathVariable Long id, @RequestBody TbContractDTO contractDTO) {
        log.info("执行商机转合同:{}, {}", id, contractDTO);
        tbContractService.changeToContract(id, contractDTO);
        return AjaxResult.success();
    }

    /**
     * 获取合同信息1/根据id查询合同详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    @ApiOperation("获取合同信息2/根据id查询合同详情")
    public AjaxResult findById1 (@PathVariable Long id) {
        log.info("执行查询合同详情:{}", id);
        TbContractVo tbContractVo = tbContractService.findById(id);
        return AjaxResult.success(tbContractVo);
    }


    /**
     * 查询合同列表  根据时间查询
     * @param contactPageQueryDTO
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询合同列表")
    public TableDataInfo<List<TbContract>> page(ContactPageQueryDTO contactPageQueryDTO) {
        log.info("执行查询合同列表:{}", contactPageQueryDTO);
        return tbContractService.pageContract(contactPageQueryDTO);
    }

    /**
     * 获取合同信息2/根据id查询合同详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("获取合同信息2/根据id查询合同详情")
    public AjaxResult findById2 (@PathVariable Long id) {
        log.info("执行查询合同详情:{}", id);
        TbContractVo tbContractVo = tbContractService.findById(id);
        return AjaxResult.success(tbContractVo);
    }

}
